import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:workmate/provider/global.dart';
import 'package:workmate/routers/handler.dart';
import 'package:workmate/routers/pages.dart';

class Routers extends FluroRouter {
  Routers() : super() {
    defineRoutes(this);
  }

  void defineRoutes(FluroRouter router) {
    router.define(Pages.home, handler: homeHandler);
    router.define(Pages.web, handler: webviewHandler);
  }

  Future goto(
    String path, {
    bool replace = false,
    bool clearStack = false,
    bool rootNavigator = false,
    bool maintainState = false,
    TransitionType transition = TransitionType.inFromRight,
    Duration? transitionDuration,
    Widget Function(BuildContext, Animation<double>, Animation<double>, Widget)?
        transitionBuilder,
    RouteSettings? routeSettings,
    BuildContext? context,
    Map<String, dynamic>? query,
  }) {
    String params = '';
    if (query != null) {
      int index = 0;
      for (var key in query.keys) {
        if (index == 0) {
          params = '?';
        } else {
          params += '&';
        }
        debugPrint('${query[key].runtimeType}runtimeType');
        var values = query[key];
        if (values.runtimeType.toString() == 'String') {
          var value = Uri.encodeComponent(values);
          params += '$key=$value';
        } else {
          int i = 0;
          for (var item in values) {
            var value = Uri.encodeComponent(item);
            if (i > 0) {
              params += '&';
            }
            params += '$key=$value';
            i++;
          }
        }
        index++;
      }
    }
    path += params;
    debugPrint('我是navigateTo传递的参数：$path');
    return super.navigateTo(context ?? APP.navigatorKey.currentContext!, path,
        replace: replace,
        clearStack: clearStack,
        maintainState: maintainState,
        transition: transition);
  }
}
